//
//  SNAssociatorViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/16.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNMyArtelAssociatorViewController.h"
#import "SNMyArtelModel.h"
#import "SNMyArtelCell.h"
#import "SNMyArtelViewController.h"
#import "SNAssociatorCell.h"
@interface SNMyArtelViewController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** ZXUserDetailInfo * userDetailInfo */
@property (nonatomic, strong) SNMyArtelResult * model;
@end

@implementation SNMyArtelViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    switch (kUserInfo.userType.integerValue) {
        case 1:
            self.navigationItem.title = @"我的组织";
            break;
            
        default:
            self.navigationItem.title = @"组织管理";
            break;
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    CGRect frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    self.tableView = tableView;
    [self setupInit:tableView];
    
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
//    
    [[ZXHTTPManager sharedManager] groupinfoWithUserId:nil organId:self.companyNo completed:^(NSDictionary *jsonDict, NSError *error) {
        
        logError;
        
        NSLog(@"%@",jsonDict);
        SNMyArtelModel * myArtelModel = [SNMyArtelModel mj_objectWithKeyValues:jsonDict];
        
        if(myArtelModel.status.integerValue != 0){
            // 数据获取失败
            return ;
        }
        
        self.model = myArtelModel.result.firstObject;
        [self.tableView reloadData];
    }];
    
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}

- (void)setupInit:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 10)];
    headerView.backgroundColor = HexRGB(0xf5f5f5);
    tableView.tableHeaderView = headerView;
    tableView.backgroundColor = HexRGB(0xf5f5f5);
    //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorInset = UIEdgeInsetsZero;
    tableView.tableFooterView = [UIView new];
    [self setHeights:tableView];
    [self registerNib:tableView];
    [self registerClass:tableView];
}
#pragma mark - <Register>
/** registerNib */
- (void)registerNib:(UITableView *)tableView {
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SNMyArtelCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SNMyArtelCell class])];
}
/** registerClass */
- (void)registerClass:(UITableView *)tableView {
    //    [tableView registerClass:[SNTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SNTableViewCell class])];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    //    [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([UITableViewHeaderFooterView class])];
}
#pragma mark - <SetHeights>
/** setHeights */
- (void)setHeights:(UITableView *)tableView {
    
        tableView.estimatedRowHeight = 47;
    tableView.rowHeight = UITableViewAutomaticDimension;
    
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SNMyArtelCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNMyArtelCell class])];
    
    [cell configWithModel:self.model indexPath:indexPath];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if (indexPath.row == 7) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.valueLabel.textColor = HexRGB(0x67B245);
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.valueLabel.textColor = HexRGB(0x000000);
    }
    
    
    return cell;
}
#pragma mark - <UITableViewDelegate>

/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    //    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 7) {
        SNMyArtelAssociatorViewController * vc = [SNMyArtelAssociatorViewController new];
        vc.memberlist = self.model.memberList;
        vc.memberqty = self.model.memberqty;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{

    }
}
/** 取消 选中 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%s", __func__);
}


@end
